﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TP_02.Models;

namespace TP_02.Controllers
{
    public class GameController : Controller
    {
        // GET: Game
        public ActionResult Index()
        {
            return View(Game.GetList());
        }

        // GET: Game/Details/5
        public ActionResult Details(int id)
        {
            return View(Game.FindOne(id));
        }

        // GET: Game/Create
        [Authorize]
        public ActionResult Create()
        {
            return View(new Game());
        }

        // POST: Game/Create
        [HttpPost]
        [Authorize]
        public ActionResult Create(Game game)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View(game);
                }

                game.SaveAsNew();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(game);
            }
        }

        // GET: Game/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            return View(Game.FindOne(id));
        }

        // POST: Game/Edit/5
        [HttpPost]
        [Authorize]
        public ActionResult Edit(int id, Game game)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View(game);
                }

                game.Update();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(game);
            }
        }

        // GET: Game/Delete/5
        [Authorize]
        public ActionResult Delete(int id)
        {
            return View(Game.FindOne(id));
        }

        // POST: Game/Delete/5
        [HttpPost]
        [Authorize]
        public ActionResult Delete(int id, Game game)
        {
            try
            {
                Game.Destroy(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(game);
            }
        }
    }
}
